using MediatR;
using Medical.Domain;
using Medical.Infrastructrue.Interfaces;
using Medical.ReadApi.Appcontext.Command;
using Medical.ReadApi.Dto;

namespace Medical.ReadApi.Appcontext.CommandHandler
{
	public class GetEmployeeByIdQueryHandler : IRequestHandler<GetEmployeeByIdQuery, EmployeeDto>
	{
		private readonly IBaseRepository<Employee> _repository;

		public GetEmployeeByIdQueryHandler(IBaseRepository<Employee> repository)
		{
			_repository = repository;
		}

		public async Task<EmployeeDto> Handle(GetEmployeeByIdQuery request, CancellationToken cancellationToken)
		{
			var entity = await _repository.GetModel(x => x.EmployeeId == request.EmployeeId && !x.IsDeleted);
			if (entity == null) throw new Exception("员工不存在");
			return new EmployeeDto
			{
				EmployeeId = entity.EmployeeId,
				UserName = entity.UserName,
				FullName = entity.FullName,
				WorkNo = entity.WorkNo,
				DepartmentId = entity.DepartmentId,
				Email = entity.Email,
				Gender = entity.Gender,
				Address = entity.Address,
				Phone = entity.Phone,
				IdCardNo = entity.IdCardNo,
				Title = entity.Title,
				Role = entity.Role,
				IsActive = entity.IsActive,
				CreatedAt = entity.CreatedAt,
				UpdatedAt = entity.UpdatedAt
			};
		}
	}
}
